package lance.honeydew.dao;

import java.util.ArrayList;
import java.util.List;

import lance.honeydew.bean.CategoryBig;
import lance.honeydew.bean.CategoryMedium;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;

public class CategoryMediumDAO extends RootDAO {
	
	private static final Logger logger = Logger.getLogger(CategoryMediumDAO.class);
	
	/**
	 * findCategoryMediumListByBigSeqId
	 * @param bigSeqId
	 * @return
	 */
	public List<CategoryMedium> findCategoryMediumListByBigSeqId(Long bigSeqId) {
		
		DetachedCriteria criteria = DetachedCriteria.forClass(CategoryMedium.class);
		List<CategoryMedium> result = null;
		
		try {
			
			String sqlString = " select m from CategoryMedium m ";
			sqlString += " inner join fetch m.Categorybigseqid b  where 1=1 ";
			sqlString += " and b.Seqid = " + bigSeqId + " ";
			
			
			Session session = getSession();
			result = (List<CategoryMedium>)session.createQuery(sqlString).list();
			session.close();
			
		} catch(Exception e) {
			e.printStackTrace();
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * findCategoryMediumBySeqId
	 * @param seqId
	 * @return
	 */
	public CategoryMedium findCategoryMediumBySeqId(Long seqId) {
		
		DetachedCriteria criteria = DetachedCriteria.forClass(CategoryMedium.class);
		CategoryMedium result = null;
		
		try {
			
			criteria.add(Restrictions.eq(CategoryMedium.PROP_SEQID, seqId));
			
			criteria.setProjection(Projections.projectionList()
					.add(Projections.property(CategoryMedium.PROP_SEQID), "_seqid")
					.add(Projections.property(CategoryMedium.PROP_NAME), "_name"));
			criteria.setResultTransformer(Transformers.aliasToBean(CategoryMedium.class));
			
			result = (CategoryMedium) this.getHibernateTemplate().
					findByCriteria(criteria).get(0);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * queryCategoryMediumByQb
	 * @param qb
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<CategoryMedium> queryCategoryMediumByQb(CategoryMedium qb, int pageNum, int pageSize) {
		
		List<CategoryMedium> list = null;
		
		try {
			
			
			String sqlString = " select m from CategoryMedium m ";
			if (qb.getCategorybigseqid() != null) {
				sqlString += " inner join fetch m.Categorybigseqid b  where 1=1 ";
			}
			
			if (StringUtils.isNotEmpty(qb.getName())) {
				sqlString += " and m.Name like '%" + qb.getName() + "%' ";
			}
			
			Session session = getSession();
			list = (List<CategoryMedium>)session.createQuery(sqlString).list();
			session.close();
			
			
		} catch(Exception e) {
			e.printStackTrace();
			logger.debug(e);
		}
		
		return list;
	}
}